package com.shuiyun.weobo.controller;

import com.shuiyun.weobo.enums.TableNameEnum;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.hadoop.hbase.HColumnDescriptor;
import org.apache.hadoop.hbase.HTableDescriptor;
import org.apache.hadoop.hbase.NamespaceDescriptor;
import org.apache.hadoop.hbase.TableName;
import org.apache.hadoop.hbase.client.Admin;
import org.apache.hadoop.hbase.client.Connection;
import org.apache.hadoop.hbase.io.compress.Compression;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @program: weibo
 * @description:
 * @author: shuiyun
 * @create: 2020-04-26 22:09
 **/
@Api(tags = "初始化命名空间和表")
@RestController
@RequestMapping("/init")
public class InitController {

    @Autowired
    private Connection connection;

    @GetMapping("/initNameSpace")
    @ApiOperation("初始化命名空间")
    public String initNameSpace() throws Exception{
        Admin admin = connection.getAdmin();
        NamespaceDescriptor namespaceDescriptor = NamespaceDescriptor.create("weibo").addConfiguration("creater", "shuiyun").build();
        admin.createNamespace(namespaceDescriptor);
        admin.close();
        return "success";
    }

    @GetMapping("/initTableContent")
    @ApiOperation("初始化微博信息表")
    public String initTableContent() throws Exception{
        Admin admin = connection.getAdmin();
        HTableDescriptor content = new HTableDescriptor(TableName.valueOf(TableNameEnum.TABLE_CONTENT));

        HColumnDescriptor info = new HColumnDescriptor("info");
        info.setMinVersions(1);
        info.setMaxVersions(1);
        info.setBlockCacheEnabled(true);
        info.setBlocksize(2048*1024);

        content.addFamily(info);
        admin.createTable(content);
        admin.close();
        return "success";
    }

    @GetMapping("/initTableRelations")
    @ApiOperation("初始化用户关系表")
    public String initTableRelations() throws Exception{
        Admin admin = connection.getAdmin();
        HTableDescriptor content = new HTableDescriptor(TableName.valueOf(TableNameEnum.TABLE_RELATIONS));

        HColumnDescriptor attends = new HColumnDescriptor("attends");
        attends.setMinVersions(1);
        attends.setMaxVersions(1);
        attends.setBlockCacheEnabled(true);
        attends.setBlocksize(2048*1024);

        HColumnDescriptor fans = new HColumnDescriptor("fans");
        fans.setMinVersions(1);
        fans.setMaxVersions(1);
        fans.setBlockCacheEnabled(true);
        fans.setBlocksize(2048*1024);


        // 设置数据压缩
        content.addFamily(attends);
        content.addFamily(fans);

        admin.createTable(content);
        admin.close();
        return "success";
    }

    @GetMapping("/initTableReceiveEmail")
    @ApiOperation("初始化微博收件箱表")
    public String initTableReceiveEmail() throws Exception{
        Admin admin = connection.getAdmin();
        HTableDescriptor content = new HTableDescriptor(TableName.valueOf(TableNameEnum.TABLE_RECEIVE_CONTENT_EMAIL));

        HColumnDescriptor info = new HColumnDescriptor("info");
        info.setMinVersions(1000);
        info.setMaxVersions(1000);
        info.setBlockCacheEnabled(true);
        info.setBlocksize(2048*1024);

        content.addFamily(info);
        admin.createTable(content);
        admin.close();
        return "success";
    }
}
